/****************************************************
          File Name:t.c
          Author: Abby Cin
          Mail: abbytsing@gmail.com
          Created Time: Wed 08 Oct 2014 08:13:24 PM CST
*****************************************************/

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
#include<sys/wait.h>

void dealing(int sig)
{
    if(sig==SIGCHLD)
    {
        while(waitpid(-1,NULL,WNOHANG)>0)
            continue;
        printf("\e[32m got SIGCHLD \e[0m\n");
    }
}

int run()
{
    sleep(2);
    printf("now return\n");
    return 0;
}

int main(int argc, char *argv[])
{
    int num;
    struct sigaction sa;

    sigemptyset(&sa.sa_mask);
    sa.sa_flags=SA_RESTART;
    sa.sa_handler=dealing;

    sigaction(SIGCHLD,&sa,NULL);

    num=3;

    while(num>0)
    { 
        num--;
        switch(fork())
        {
            case -1:
                exit(1);
            case 0:
                run();
                _exit(0);
        }
    }
    for(;;)
        pause();
}
